<?php
$flag = 0;
$total_money = 0;
$f = false;
$k = 0;
$arr_odd_bets = array(
    "ODD_BETS" => array(
        "ODD_FT" => array(
            "0" => "Chủ",
            "1" => "Khách",
            "2" => "Hoà"
        ),
        "ODD_FTHDP" => array(
            "0" => "Tỷ Lệ",
            "1" => "Chủ",
            "2" => "Khách",
        ),
        "ODD_R1" => array(
            "0" => "Chủ",
            "1" => "Khách",
            "2" => "Hoà"
        ),
        "ODD_HDPR1" => array(
            "0" => "Tỷ Lệ",
            "1" => "Chủ",
            "2" => "Khách",
        ),
        "ODD_UO" => array(
            "0" => "Tỷ Lệ",
            "1" => "Trên",
            "2" => "Dưới",
        ),
        "ODD_UOR1" => array(
            "0" => "Tỷ Lệ",
            "1" => "Trên",
            "2" => "Dưới",
        ),
        "ODD_TG" => array(
            "0" => "0-1",
            "1" => "2-3",
            "2" => "4-6",
            "3" => "Từ 7 trở lên",
        ),
        "ODD_3RHDP" => array(
            "0" => "Chủ (+1)",
            "1" => "Hoà (+1)",
            "2" => "Khách (-1)",
        ),
        "ODD_WL" => array(
            "1" => "Chủ",
            "2" => "Khách",
        ),
        "ODD_CL" => array(
            "0" => "Chẵn",
            "1" => "Lẻ",
        ),
        "ODD_WCG" => array(
            "0" => "Chủ",
            "1" => "Khách",
        ),
        "ODD_2W" => array(
            "0" => "Chủ / Hoà",
            "1" => "Chủ / Khách",
            "2" => "Khách / Hoà ",
        ),
        "ODD_CG" => array(
            "0" => "Chủ - Có",
            "1" => "Chủ - Không",
            "2" => "Khách - Có",
            "3" => "Khách - Không",
        ),
        "ODD_WG" => array(
            "0" => "Cả 2 đội",
            "1" => "Chủ",
            "2" => "Không đội nào",
            "3" => "Khách",
        ), 
    )
);
?>
<form action="" id="verifyForm" class="form-horizontal" method="post">

    <?php
    if (isset($_SESSION["cart"])) {
        ksort($_SESSION["cart"]);
        foreach ($_SESSION["cart"] as $__key => $__cart) {
            ?>
            <legend><?php if ($__key == "bongda") { ?>Dự Đoán Kết Quả Bóng Đá<?php } else { ?>Dự Đoán Kết Quả Sổ Xố ( Đề : 1 coins = 1 điểm - Lô 20 coins = 1 điểm )<?php } ?></legend>
            <div id="message" class="alert alert-error" style="<?php
            if ($flag == 1) {
                echo "display: block;";
            } else {
                echo "display: none;";
            }
            ?>"><?php
                     if ($flag == 1) {
                         echo "Tài khoản bạn không đủ tiền.";
                     }
                     ?></div>
            <table class="table table-bordered table-events">

                <thead>
                    <?php if ($__key == "bongda") { ?>
                        <tr class="heading">
                            <th>Trận đấu</th>
                            <th>Chọn</th>
                            <th>Kiểu</th>
                            <th style="width: 63px;">Đặt</th>
                            <th style="width: 63px;">Thắng</th>
                            <th style="width: 85px;"></th>
                        </tr>
                    <?php } else if ($__key == "soxo") { ?>
                        <tr class="heading">
                            <th>Chọn Số</th>
                            <th>Miền</th>
                            <th>Kiểu</th>
                            <th style="width: 63px;">Đặt</th>
                            <th style="width: 63px;">Thắng</th>
                            <th style="width: 85px;"></th>
                        </tr>
                    <?php } else { ?>
                        <tr class="heading">
                        </tr>
                    <?php } ?>
                </thead>
                <tbody>
                    <?php
                    if ($__key == "bongda") {
                        foreach ($_SESSION["cart"]["bongda"] as $keys => $_cart) {
                            $base_decode = base64_decode($_cart);
                            $json_decode = json_decode($base_decode);
                            $match = Frontend::findMatchById((int) $json_decode->match_id);
                            foreach ($match as $_match) {
                                $team1 = Frontend::findTeamById((int) $_match["Team1"]);
                                $team2 = Frontend::findTeamById((int) $_match["Team2"]);
                                $odd_value = Frontend::findOddById((int) $json_decode->odd_id);
                                foreach ($odd_value as $_odd_value) {
                                    $value = explode("|", $_odd_value["value"]);
                                    foreach ($arr_odd_bets as $_arr_odd_bets) {
                                        foreach ($value as $key => $_value) {
                                            if ($_value == $json_decode->odd_value) {
                                                if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 2) {
                                                    if ($key > 2) {
                                                        if ($key % 2 == 0) {
                                                            $k = 0;
                                                        }
                                                        if ($key % 2 == 1) {
                                                            $k = 1;
                                                        }
                                                    } else {
                                                        $k = $key;
                                                    }
                                                } else if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 3) {
                                                    if ($key > 3) {
                                                        if ($key % 3 == 0) {
                                                            $k = 0;
                                                        }
                                                        if ($key % 3 == 1) {
                                                            $k = 1;
                                                        }
                                                        if ($key % 3 == 2) {
                                                            $k = 2;
                                                        }
                                                    } else {
                                                        $k = $key;
                                                    }
                                                } else if (count($_arr_odd_bets[$_odd_value["typeID"]]) == 4) {
                                                    if ($key > 4) {
                                                        if ($key % 4 == 0) {
                                                            $k = 0;
                                                        }
                                                        if ($key % 4 == 1) {
                                                            $k = 1;
                                                        }
                                                        if ($key % 4 == 2) {
                                                            $k = 2;
                                                        }
                                                        if ($key % 4 == 3) {
                                                            $k = 3;
                                                        }
                                                    } else {
                                                        $k = $key;
                                                    }
                                                }
                                                var_export($k);
                                                $chon_kieu = $_odd_value["title"];
                                                $chon_cua = $_arr_odd_bets[$_odd_value["typeID"]][$k];
                                                $odd_id = $_odd_value["_id"];
                                                $index_key = $key;
                                            }
                                        }
                                    }
                                }
                                ?>
                                <tr class="alert">
                                    <td style="text-align: left;">
                                        <a href="#">
                                            <?php
                                            foreach ($team1 as $_team1)
                                                echo $_team1["name"] . " - ";
                                            foreach ($team2 as $_team2)
                                                echo $_team2["name"];
                                            ?></a>
                                    </td>
                                    <td style="text-align: left;"><?php echo $chon_cua ?><span class="label label-warning" style="margin-left: 10px"><?php echo $json_decode->odd_value; ?></span></td>
                                    <td style="text-align: left;"><?php echo $chon_kieu ?><input type="hidden" name="index_key[<?php echo $__key . "_" . $keys ?>]" value="<?php echo $index_key; ?>"/></td>
                                    <td>
                                        <input id="number_money_<?php echo $__key . "_" . $keys ?>" name="number_money[<?php echo $__key . "_" . $keys ?>]" type="text" maxlength="4" value="<?php
                                        if ($json_decode->money)
                                            echo $json_decode->money;
                                        else
                                            echo "0";
                                        ?>" style="width: 50px" onkeydown="checknumber('<?php echo $keys ?>')"/>
                                        <!--<a class="btn bt-log" data-toggle="modal" role="button" href="#login" type="submit">Đăng nhập</a>-->
                                    </td>
                                    <td>
                                        <input id="total_money_<?php echo $__key . "_" . $keys ?>" readonly="" type="text" value="<?php
                                        if ($json_decode->total_money)
                                            echo $json_decode->total_money;
                                        else
                                            echo "0";
                                        ?>" style="width: 65px"/>
                                    </td>
                                    <td>
                                        <a id="btnupdate" onclick="update_datcuoc('<?php echo $__key ?>', '<?php echo $_cart ?>', '<?php echo $keys ?>', '<?php echo $index_key; ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/update.png"/></a>
                                        <a id="btndelete" onclick="delete_datcuoc('<?php echo $__key ?>', '<?php echo $keys ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/delete.png"/></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        <?php } ?>
                        <?php if (!isset($_SESSION["cart"]["soxo"])) { ?>
                            <tr class="alert">
                                <td colspan="6" style="text-align: right">
                                    <button type="submit" class="btn btn-inverse btn-large" id="btndatcuoc" style="padding: 7px 9px; font-size: 15px">Đặt cược</button>
                                </td>
                            </tr>
                        <?php } ?>
                        <?php
                    } else if ($__key == "soxo") {
                        foreach ($_SESSION["cart"]["soxo"] as $keys => $_cart) {
                            $base_decode = base64_decode($_cart);
                            $json_decode = json_decode($base_decode);
                            ?>
                            <tr class="alert">
                                <td style="text-align: left;">
                                    <?php echo $json_decode->value ?>
                                </td>
                                <td style="text-align: left;"><?php
                                    if ($json_decode->location == 1) {
                                        echo "Bắc";
                                    } else {
                                        echo "Nam";
                                    }
                                    ?></td>
                                <td style="text-align: left;"><?php
                                    if ($json_decode->type == 1) {
                                        echo "Đề";
                                    } else {
                                        echo "Lô";
                                    }
                                    ?></td>
                                <td>
                                    <input id="number_money_<?php echo $__key . "_" . $keys ?>" name="number_money[<?php echo $__key . "_" . $keys ?>]" type="text" maxlength="4" value="<?php
                                    if ($json_decode->money)
                                        echo $json_decode->money;
                                    else
                                        echo "0";
                                    ?>" style="width: 50px" onkeydown="checknumber('<?php echo $keys ?>')"/>
                                    <!--<a class="btn bt-log" data-toggle="modal" role="button" href="#login" type="submit">Đăng nhập</a>-->
                                </td>
                                <td>
                                    <input id="total_money_<?php echo $__key . "_" . $keys ?>" readonly="" type="text" value="<?php
                                    if ($json_decode->total_money)
                                        echo $json_decode->total_money;
                                    else
                                        echo "0";
                                    ?>" style="width: 65px"/>
                                </td>
                                <td>
                                    <a id="btnupdate" onclick="update_datcuoc('<?php echo $__key ?>', '<?php echo $_cart ?>', '<?php echo $keys ?>', '')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/update.png"/></a>
                                    <a id="btndelete" onclick="delete_datcuoc('<?php echo $__key ?>', '<?php echo $keys ?>')" href="javascript:void(0)"><img src="<?php echo SKIN_FPATH ?>skin/frontend/icon/delete.png"/></a>
                                </td>
                            </tr>
                        <?php } ?>
                        <tr class="alert">
                            <!--<td colspan="5" style="text-align: right"><b>Tổng điểm thắng : </b><input id="total_money" readonly="" type="text" value="" style="width: 65px"/></td>-->
                            <td colspan="6" style="text-align: right">
                                <button type="submit" class="btn btn-inverse btn-large" id="btndatcuoc" style="padding: 7px 9px; font-size: 15px">Đặt cược</button>
                            </td>
                        </tr>
                    <?php } ?>
                </tbody>
            </table>
            <?php
        }
    } else {
        ?>
        <table class="table table-bordered table-events">
            <tbody>
                <tr class="alert">
                    <td><?php
                        if ($flag == 2) {
                            echo "Dự đoán thành công.";
                        } else if ($flag == 1) {
                            echo "Tài khoản bạn không đủ tiền.";
                        } else {
                            echo "Chưa có dự đoán nào.";
                        }
                        ?></td>
                </tr>
            </tbody>
        </table>        
        <?php
    }
    ?>
</form>